package com.xydtech.ecard.flow.controller;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.busiflw.domain.BusiflDocList;
import com.xydtech.busiflw.domain.BusiflDocReg;
import com.xydtech.busiflw.service.IBusiflDocListService;
import com.xydtech.busiflw.service.IBusiflDocRegService;
import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.config.XydtechBusiPlatConfig;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.common.utils.file.FileUploadUtils;
import com.xydtech.common.utils.file.MimeTypeUtils;
import com.xydtech.common.utils.uuid.IdUtils;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @program: XydtechBusiPlat
 * @ClassName: EcardApiBusiflDocRegController
 * @description:流程中-影像资料
 * @author: yjs
 * @create: 2024-10-30 15:05
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务流程")
@RequestMapping("/ecardSaasApi/businessDoc")
public class EcardApiBusiflDocRegController extends EcardApiBaseController {
    @Autowired
    private IBusiflDocRegService busiflDocRegService;
    @Autowired
    private IBusiflDocListService busiflDocListService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;

    /**
     * @description：任务-影像资料-获取影像资料类型列表
     * @author：yjs
     * @date：2024/10/30 15:08
     */
    @ApiOperation(value = "任务-影像资料-获取影像资料类型列表", notes = "任务-影像资料-获取影像资料类型列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = AjaxResult.class)
    })
    @PostMapping("/getDocRegList")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getDocRegList(@RequestBody BusiflDocReg busiflDocReg) {
        List<BusiflDocReg> list = busiflDocRegService.selectBusiflDocRegList(busiflDocReg);
        return ResponseModel.okToJSONArray(list);
    }

    /**
     * @description：任务-影像资料-获取影像资料列表
     * @author：yjs
     * @date：2024/10/30 15:09
     */
    @ApiOperation(value = "任务-影像资料-获取影像资料列表", notes = "任务-影像资料-获取影像资料列表")
    @PostMapping("/getDocList")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel getDocList(@RequestBody String json) {
        Long docRegId = Long.valueOf(JSONObject.parseObject(json).getString("docRegId"));
        BusiflDocList busiflDocList = new BusiflDocList();
        busiflDocList.setDocRegId(docRegId);
        List<BusiflDocList> list = busiflDocListService.selectBusiflDocListList(busiflDocList);
        return ResponseModel.okToJSONArray(list);
    }

    /**
     * @param docRegId   档案编号
     * @param instanceId 流程实例ID
     * @param docCode    档案编码
     * @param file       影像文件
     * @description：任务-影像资料-上传影像资料
     * @author：yjs
     * @date：2024/10/30 15:09
     */
    @ApiOperation(value = "任务-影像资料-上传影像资料", notes = "任务-影像资料-上传影像资料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "docRegId", value = "档案编号", dataType = "Long", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "instanceId", value = "流程实例ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "docCode", value = "档案编码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "location", value = "水印内容", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "file", value = "影像文件", dataType = "MultipartFile", dataTypeClass = MultipartFile.class)
    })
    @PostMapping("/uploadDocFile")
    @ResponseBody
    @RsaRequest(param = true, result = true)
    public ResponseModel uploadDocFile(Long docRegId, String instanceId, String docCode, String location, @RequestParam("file") MultipartFile file) {

        try {
            String salt = getSalt();//盐值
            //获取当前用户的信息
            EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
            //图片验证
            if (!file.isEmpty()) {
                FileUploadUtils.uploadCheckType(file, MimeTypeUtils.IMAGE_EXTENSION);
                int index = file.getOriginalFilename().lastIndexOf(".");
                //文件类型
                String fileType = file.getOriginalFilename().substring(index + 1);
                //文件名称
                String fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID();
                // 上传文件路径
                String filePath = XydtechBusiPlatConfig.getProfile() + "/" + userInfo.getDeptId() + "/busifl/" + instanceId + "/" + docCode+ "/" + fileName + "." + fileType;
                logger.info("文件路径========" + filePath);
                File imgfile = new File(filePath);
                if (!imgfile.getParentFile().exists()) {
                    //创建上级目录
                    imgfile.getParentFile().mkdirs();
                }
                //图片添加水印后保存
                addBusiflWaterMask(file, filePath, "影像资料", userInfo.getUserName(), userInfo.getDeptName());
                String fileUrl = filePath.replaceAll(XydtechBusiPlatConfig.getProfile(), "/profile");
                BusiflDocList busiflDocList = new BusiflDocList();
                busiflDocList.setDocRegId(docRegId);
                busiflDocList.setUserId(userInfo.getUserId());
                busiflDocList.setFileName(fileName);
                busiflDocList.setFileType(fileType);
                busiflDocList.setFileUrl(fileUrl);
                busiflDocListService.insertBusiflDocList(busiflDocList);
                return ResponseModel.okToJSON(busiflDocList);
            } else {
                return ResponseModel.error("图片不能为空！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseModel.error(e.getMessage());
        }
    }

    /**
     * @description：任务-影像资料-删除影像资料文件
     * @author：yjs
     * @date：2024/10/30 15:49
     */
    @ApiOperation(value = "任务-影像资料-删除影像资料文件", notes = "任务-影像资料-删除影像资料文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "影像文件ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "docRegId", value = "档案编号", dataType = "Long", dataTypeClass = Long.class)
    })
    @PostMapping("/delDocList")
    @ResponseBody
    @RsaRequest(param = true, result = false)
    public ResponseModel delDocList(@RequestBody String json) {
        Long id = Long.valueOf(JSONObject.parseObject(json).getString("id"));
        Long docRegId = Long.valueOf(JSONObject.parseObject(json).getString("docRegId"));
        return ResponseModel.ok(busiflDocListService.deleteBusiflDocListById(id, docRegId));
    }
}
